package com.noname.lib_base_java.util;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Author: Lmoumou
 * Date: 2018-10-15 16:42
 * 文件名: TimeUtils
 * 描述:
 */
public class TimeUtils {

    public static final String FORMAT_YMDHMS_ONE = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_YMD_ONE = "yyyy-MM-dd";
    public static final String FORMAT_YMDHMS_TWO = "yyyy年MM月dd日 HH时mm分ss秒";
    public static final String FORMAT_YMD_TWO = "yyyy年MM月dd日";
    public static final String FORMAT_YMDHM_ONE = "yyyy-MM-dd HH:mm";
    @SuppressLint("SimpleDateFormat")
    private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
    @SuppressLint("SimpleDateFormat")
    private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
    @SuppressLint("SimpleDateFormat")
    private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    /**
     * 时间戳转String
     */
    @SuppressLint("SimpleDateFormat")
    public static String long2String(long timeMillis, String format) {
        if (format == null) {
            format = FORMAT_YMD_ONE;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(timeMillis);
    }

    /**
     * 日期转时间戳
     */
    public static Long str2Long(String dateStr, String formatStr) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatStr, Locale.CHINA);
            Date parse = format.parse(dateStr);

            return parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return 0L;
    }

    /**
     * 根据格式转日期
     *
     * @param format
     * @param dateStr
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static Date getDateByStr(String dateStr, String format) {
        if (dateStr == null || TextUtils.isEmpty(dateStr)) {
            return Calendar.getInstance().getTime();
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        return date;
    }

    /**
     * 获取指定年月
     */
    public static String getMonth(Long l) {
        Calendar c = Calendar.getInstance();
        Date date = new Date(l);
        c.setTime(date);
        c.add(Calendar.MONTH, 0);
        Date time = c.getTime();

        return long2String(time.getTime(), "yyyy年MM月");
    }

    /**
     * 获取上一个月
     */
    public static String getLeftMoth(Long l) {
        Calendar c = Calendar.getInstance();
        Date date = new Date(l);
        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        Date time = c.getTime();
        return long2String(time.getTime(), "yyyy-MM");
    }

    /**
     * 获取下一个月
     */
    public static String getRightMonth(Long l) {
        Calendar c = Calendar.getInstance();
        Date date = new Date(l);
        c.setTime(date);
        c.add(Calendar.MONTH, +1);
        Date time = c.getTime();
        return long2String(time.getTime(), "yyyy-MM");
    }

    /**
     * 获取季度
     *
     * @param l
     * @return
     */
    public static int getCurrentQuarterStartTime(long l) {
        Calendar c = Calendar.getInstance();
        Date date = new Date(l);
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        int now = 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                now = 1;
            else if (currentMonth >= 4 && currentMonth <= 6)
                now = 2;
            else if (currentMonth >= 7 && currentMonth <= 9)
                now = 3;
            else if (currentMonth >= 10 && currentMonth <= 12)
                now = 4;
            c.set(Calendar.DATE, 1);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 获取季度结束时间
     *
     * @param l
     * @return
     */
    public static long getQuarterEndTime(long l) {
        Calendar c = Calendar.getInstance();
        Date date = new Date(l);
        c.setTime(date);
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now.getTime();
    }

    /**
     * 获取季度开始时间
     *
     * @param l
     * @return
     */
    public static long getQuarterStartTime(long l) {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now.getTime();
    }

    /**
     * 获取两个时间之间的天数
     */
    public static int getDifferenceDay(long start, long end) {
        int days = 0;
        try {
            long diff = start - end;
            days = (int) (diff / (1000 * 60 * 60 * 24));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 获取是几号
     */
    public static int getMothDay(Long aLong) {
        Calendar c = Calendar.getInstance();
        Date date = new Date(aLong);
        c.setTime(date);
        int day = c.get(Calendar.DAY_OF_MONTH);
        L.e("LLSSQQ", "day->" + day);
        return day;
    }


}
